
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubTTMessage = exports.CSChatMessage2 = exports.CSChatMessage = exports.PubChatMessage2 = exports.PubChatMessage = exports.GroupChatMessage = exports.ChatMessage2 = exports.ChatMessage = void 0;
var sessionIdProcessor_1 = __webpack_require__(/*! _Eutils/sessionIdProcessor */ "./src/extra/utils/sessionIdProcessor.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var long_1 = __webpack_require__(/*! _util/long */ "./src/core/utils/long.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/extra/utils/log.ts");
var Message = /** @class */ (function () {
    function Message(_a) {
        var _b = _a.appId, appId = _b === void 0 ? 0 : _b, _c = _a.channelId, channelId = _c === void 0 ? 0 : _c, _d = _a.type, type = _d === void 0 ? Message.TYPE.MSG_TYPE_TEXT : _d, _e = _a.uuid, uuid = _e === void 0 ? '' : _e, _f = _a.from, from = _f === void 0 ? '' : _f, _g = _a.fromName, fromName = _g === void 0 ? '' : _g, _h = _a.to, to = _h === void 0 ? '' : _h, _j = _a.time, time = _j === void 0 ? 0 : _j, _k = _a.status, status = _k === void 0 ? constant_1.MESSAGE_STATUS.STATUS_CREATED : _k, _l = _a.belongTo, belongTo = _l === void 0 ? '' : _l, _m = _a.mid, mid = _m === void 0 ? '' : _m, _o = _a.extension, extension = _o === void 0 ? '' : _o, _p = _a.deviceType, deviceType = _p === void 0 ? 0 : _p, message = _a.message, _q = _a.isLocal, isLocal = _q === void 0 ? false : _q, _r = _a.toAppId, toAppId = _r === void 0 ? 0 : _r, sessionSeqId = _a.sessionSeqId, compatible = _a.compatible, _s = _a.receipt, receipt = _s === void 0 ? false : _s, _t = _a.direction, direction = _t === void 0 ? 1 : _t, sid = _a.sid;
        this.sessionId = '';
        this.appId = 0;
        this.channelId = 0;
        this.type = 0;
        this.uuid = '';
        this.from = '0';
        this.fromName = '';
        this.to = '0'; // 单聊表示聊天对方的大象uid，群聊表示群id，公众号用户侧表示公众号id，公众号客服侧表示聊天对方的大象uid
        this.time = 0;
        this.status = 0;
        this.deviceType = 0;
        this.belongTo = '0'; // 表示SessionIdProcessor类里面的uid
        this.mid = '';
        this.extension = '';
        this.sessionType = '';
        this.body = {};
        this.svrTime = 0;
        this.isLocal = false;
        this.seqId = 0;
        this.isCancel = false;
        this.isTTMessage = false;
        this.toAppId = 0;
        this.pubId = ''; // 只有公众号客服侧才有值，表示公众号的id
        this.peerUid = ''; // 只有公众号客服侧才有值，表示聊天对方的大象uid
        this.sessionSeqId = '';
        this.compatible = '';
        this.receipt = false;
        this.fromPubId = ''; // 只有服务端通过公众号给群里指定人发消息时此字段才有值
        this.fromPubName = ''; // 只有服务端通过公众号给群里指定人发消息时此字段才有值
        this.sendCode = 0; // 服务端返回的消息发送结果code
        this.direction = 1; // 1表示发送的消息，2表示接收到消息。 此字段只有在pub会话时才能用
        this.appId = appId;
        this.channelId = channelId;
        this.type = type;
        this.uuid = uuid;
        this.from = from;
        this.fromName = fromName;
        this.to = to;
        this.time = time;
        this.status = status;
        this.deviceType = deviceType;
        this.belongTo = belongTo;
        this.mid = mid;
        this.extension = extension;
        this.sessionType = null;
        this.body = Message.getMessageBody(type, message);
        this.svrTime = mid ? (long_1.default.fromString(mid).shiftRight(22).toNumber() + 1361753741828) : 0;
        this.isLocal = isLocal;
        this.toAppId = toAppId;
        this.sessionSeqId = sessionSeqId;
        this.compatible = compatible;
        this.receipt = receipt;
        this.sid = sid;
        this.direction = direction;
        this.sessionId = sessionIdProcessor_1.default.create({ uid: this.belongTo, channelId: this.channelId, sid: this.sid });
    }
    Message.getMessageBody = function (type, message) {
        if (message === void 0) { message = {}; }
        if (!type || !message) {
            return {};
        }
        switch (type) {
            case Message.TYPE.MSG_TYPE_TEXT: {
                return {
                    text: message.text
                };
            }
            case Message.TYPE.MSG_TYPE_VOICE: {
                return {
                    url: message.url,
                    codec: message.codec,
                    duration: message.duration,
                    stamp: message.stamp,
                    token: message.token,
                    custom: message.custom
                };
            }
            case Message.TYPE.MSG_TYPE_VIDEO: {
                return {
                    videoUrl: message.videoUrl,
                    screenshotUrl: message.screenshotUrl,
                    duration: message.duration,
                    size: message.size,
                    width: message.width,
                    height: message.height,
                    timestamp: message.timestamp,
                    token: message.token //文件验证token
                };
            }
            case Message.TYPE.MSG_TYPE_IMAGE: {
                return {
                    thumbnail: message.thumbnail,
                    normal: message.normal,
                    original: message.original,
                    imgType: message.type,
                    token: message.token,
                    originSize: message.originSize,
                    uploadImageType: message.uploadImageType
                };
            }
            case Message.TYPE.MSG_TYPE_CALENDAR: {
                var participant = [];
                try {
                    participant = typeof message.participant && message.participant ? JSON.parse(message.participant) : [];
                }
                catch (e) {
                    log_1.default.log('MSG_TYPE_CALENDAR parse error:' + e + ' participant:' + message.participant);
                }
                return {
                    cid: message.calendarID,
                    dtstart: message.dtstart,
                    dtend: message.dtend,
                    summary: message.summary,
                    location: message.location,
                    participant: participant,
                    remark: message.remark || '',
                    trigger: message.trigger
                };
            }
            case Message.TYPE.MSG_TYPE_LINK: {
                return {
                    title: message.title,
                    content: message.content,
                    image: message.image,
                    link: message.link
                };
            }
            case Message.TYPE.MSG_TYPE_MULTI_LINK: {
                var content = [];
                try {
                    content = typeof message.content === 'string' && message.content ? JSON.parse(message.content) : [];
                }
                catch (e) {
                    log_1.default.log('MSG_TYPE_MULTI_LINK parse error:' + e + ' content:' + message.content);
                }
                return {
                    num: message.num,
                    content: content
                };
            }
            case Message.TYPE.MSG_TYPE_FILE: {
                return {
                    id: message.id,
                    url: message.url,
                    name: message.name,
                    format: message.format,
                    size: message.size,
                    token: message.token,
                    lsize: message.lsize,
                    custom: message.custom
                };
            }
            case Message.TYPE.MSG_TYPE_GPS: {
                return {
                    latitude: message.latitude,
                    longitude: message.longitude,
                    name: message.name
                };
            }
            case Message.TYPE.MSG_TYPE_VCARD: {
                return {
                    uid: message.uid,
                    name: message.name,
                    account: message.account,
                    eType: message.type,
                    type: message.type,
                    subType: message.subtype
                };
            }
            case Message.TYPE.MSG_TYPE_EMOTION: {
                return {
                    category: message.category,
                    eType: message.type,
                    type: message.type,
                    name: message.name
                };
            }
            case Message.TYPE.MSG_TYPE_EVENT: {
                return {
                    eType: message.type,
                    type: message.type,
                    text: message.text
                };
            }
            case Message.TYPE.MSG_TYPE_CUSTOM: {
                return {
                    templateName: message.templateName,
                    contentTitle: message.contentTitle,
                    content: message.content,
                    linkName: message.linkName,
                    link: message.link
                };
            }
            case Message.TYPE.MSG_TYPE_GENERAL: {
                return {
                    data: message.data,
                    eType: message.type,
                    type: message.type,
                    summary: message.summary
                };
            }
            case Message.TYPE.MSG_TYPE_NEWEMOTION: {
                return {
                    category: message.category,
                    packageId: message.packageId,
                    packageName: message.packageName,
                    id: message.id,
                    name: message.name,
                    eType: message.type,
                    type: message.type,
                    params: message.params
                };
            }
            case Message.TYPE.MSG_TYPE_QUOTE: {
                return {
                    lastReply: message.lastReply,
                    quoted: message.quoted,
                    searchText: message.searchText,
                    lastReplyMessage: message.lastReplyMessage,
                    quotedMessage: message.quotedMessage,
                };
            }
            case Message.TYPE.MSG_TYPE_GVCARD: {
                return {
                    guid: message.guid,
                    name: message.name,
                    account: message.account,
                    eType: message.type,
                    type: message.type,
                    subtype: message.subtype,
                };
            }
            case Message.TYPE.MSG_TYPE_NOTICE: {
                return {
                    title: message.title,
                    image: message.image,
                    content: message.content,
                    link: message.link,
                };
            }
            case Message.TYPE.MSG_TYPE_RED_PACKET: {
                return {
                    rpid: message.rpid,
                    eType: message.type,
                    type: message.type,
                    greetings: message.greetings,
                };
            }
            case Message.TYPE.MSG_TYPE_PCALL: {
                return {
                    uid: message.uid,
                    peerUid: message.peerUid,
                    roles: message.roles,
                    callStatus: message.callStatus,
                    callType: message.callType,
                    startCallTs: message.startCallTs,
                    startTalkTs: message.startTalkTs,
                    endTs: message.endTs,
                    callDur: message.callDur,
                };
            }
            case Message.TYPE.MSG_TYPE_DYNAMIC: {
                return {
                    id: message.id,
                    title: message.title,
                    dxData: message.dxData,
                    appData: message.appData,
                };
            }
            case Message.TYPE.MSG_TYPE_TT: {
                return {};
            }
        }
    };
    Message.prototype.setSeqId = function (seqId) {
        this.seqId = long_1.default.fromString(seqId) || null;
    };
    Message.prototype.setMid = function (msgId) {
        if (msgId === '0') {
            // 消息 mid 为 0 表示消息发送失败,不需要设置 mid,已经在设置地方增加判断,此处增加二重判断避免后续遗漏
            return;
        }
        var mid = msgId.toString ? msgId.toString() : msgId;
        this.mid = mid;
        this.svrTime = mid ? (long_1.default.fromString(mid).shiftRight(22).toNumber() + 1361753741828) : 0;
    };
    Message.prototype.isChatMessage = function () {
        return this.sessionType === constant_1.SESSION_TYPE.CHAT;
    };
    Message.prototype.isChatMessage2 = function () {
        return this.sessionType === constant_1.SESSION_TYPE.CHAT2;
    };
    Message.prototype.isGroupChatMessage = function () {
        return this.sessionType === constant_1.SESSION_TYPE.GROUP_CHAT;
    };
    Message.prototype.isPubChatMessage = function () {
        return this.sessionType === constant_1.SESSION_TYPE.PUB_CHAT;
    };
    Message.prototype.isPubChatMessage2 = function () {
        return this.sessionType === constant_1.SESSION_TYPE.PUB_CHAT2;
    };
    Message.prototype.isCSChatMessage = function () {
        return this.sessionType === constant_1.SESSION_TYPE.CS_CHAT;
    };
    Message.prototype.isCSChatMessage2 = function () {
        return this.sessionType === constant_1.SESSION_TYPE.CS_CHAT2;
    };
    Message.prototype.isDataMessage = function () {
        return false;
    };
    Message.prototype.toString = function () {
        return 'Message{'
            + ' sessionId:' + this.sessionId
            + ' appId:' + this.appId
            + ' channelId:' + this.channelId
            + ' type:' + this.type
            + ' uuid:' + this.uuid
            + ' from:' + this.from
            + ' fromName:' + this.fromName
            + ' to:' + this.to
            + ' time:' + this.time
            + ' status:' + this.status
            + ' deviceType:' + this.deviceType
            + ' belongTo:' + this.belongTo
            + ' mid:' + this.mid
            + ' extension:' + this.extension
            + ' sessionType:' + this.sessionType
            + ' svrTime:' + this.svrTime
            + ' isLocal:' + this.isLocal
            + ' seqId:' + this.seqId
            + ' isCancel:' + this.isCancel
            + ' isTTMessage:' + this.isTTMessage
            + ' toAppId:' + this.toAppId
            + ' pubId:' + this.pubId
            + ' peerUid:' + this.peerUid
            + ' sessionSeqId:' + this.sessionSeqId
            + ' receipt:' + this.receipt
            + ' sid:' + this.sid
            + ' fromPubId:' + this.fromPubId
            + ' fromPubName:' + this.fromPubName
            + ' }';
    };
    Message.TYPE = constant_1.MESSAGE_TYPE;
    Message.STATUS = constant_1.MESSAGE_STATUS;
    return Message;
}());
exports.default = Message;
var ChatMessage = /** @class */ (function (_super) {
    __extends(ChatMessage, _super);
    function ChatMessage(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.CHAT;
        _this.seqId = messageDatum.seqId;
        return _this;
    }
    return ChatMessage;
}(Message));
exports.ChatMessage = ChatMessage;
var ChatMessage2 = /** @class */ (function (_super) {
    __extends(ChatMessage2, _super);
    function ChatMessage2(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.CHAT2;
        _this.seqId = messageDatum.seqId;
        return _this;
    }
    return ChatMessage2;
}(Message));
exports.ChatMessage2 = ChatMessage2;
var GroupChatMessage = /** @class */ (function (_super) {
    __extends(GroupChatMessage, _super);
    function GroupChatMessage(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.groupName = '';
        _this.groupName = messageDatum.groupName || '';
        _this.sessionType = constant_1.SESSION_TYPE.GROUP_CHAT;
        _this.seqId = messageDatum.seqId;
        _this.fromPubId = messageDatum.fromPubId;
        _this.fromPubName = messageDatum.fromPubName;
        return _this;
    }
    return GroupChatMessage;
}(Message));
exports.GroupChatMessage = GroupChatMessage;
var PubChatMessage = /** @class */ (function (_super) {
    __extends(PubChatMessage, _super);
    function PubChatMessage(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.PUB_CHAT;
        return _this;
    }
    return PubChatMessage;
}(Message));
exports.PubChatMessage = PubChatMessage;
var PubChatMessage2 = /** @class */ (function (_super) {
    __extends(PubChatMessage2, _super);
    function PubChatMessage2(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.PUB_CHAT2;
        return _this;
    }
    return PubChatMessage2;
}(Message));
exports.PubChatMessage2 = PubChatMessage2;
var CSChatMessage = /** @class */ (function (_super) {
    __extends(CSChatMessage, _super);
    function CSChatMessage(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.CS_CHAT;
        _this.pubId = messageDatum.pubId;
        _this.peerUid = messageDatum.peerUid;
        return _this;
    }
    return CSChatMessage;
}(Message));
exports.CSChatMessage = CSChatMessage;
var CSChatMessage2 = /** @class */ (function (_super) {
    __extends(CSChatMessage2, _super);
    function CSChatMessage2(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.sessionType = constant_1.SESSION_TYPE.CS_CHAT2;
        _this.pubId = messageDatum.pubId;
        _this.peerUid = messageDatum.peerUid;
        return _this;
    }
    return CSChatMessage2;
}(Message));
exports.CSChatMessage2 = CSChatMessage2;
var PubTTMessage = /** @class */ (function (_super) {
    __extends(PubTTMessage, _super);
    function PubTTMessage(messageDatum) {
        var _this = _super.call(this, messageDatum) || this;
        _this.isTTMessage = true;
        _this.body = messageDatum.message;
        return _this;
    }
    return PubTTMessage;
}(Message));
exports.PubTTMessage = PubTTMessage;


//# sourceURL=webpack://MTDX/./src/extra/models/message.ts?